const api = 'https://weixin.policyol.com/'
// const api = 'http://169.254.235.17:8080'
// const api = 'http://3709c389q7.qicp.vip'

function Loginpost(url, data) {
	var promise = new Promise((resolve, reject) => {
		uni.request({
			url: api + url,
			method: 'POST',
			data: data,
			header: {
				'Content-Type': 'application/json;charset=UTF-8',
			},
			success: res => {
				resolve(res)
			},
			fail: (err) => {  
				resolve(err)
			},
		})
	})
	return promise
}

function getaction(url, adds) {
	var promise = new Promise((resolve, reject) => {
		let token = uni.getStorageSync("token")
		let userinfo = uni.getStorageSync("userinfo")
		if (!token) {
			uni.navigateTo({
				url: './pages/login/login',
			});
		}
		uni.request({
			url: api + url,
			method: 'GET',
			data: adds,
			header: {
				'Content-Type': 'application/json;charset=UTF-8',
				'X-Access-Token': token,
			},
			success: res => {
				// uni.hideLoading()
				resolve(res)
			},
			fail: (err) => {
				// uni.hideLoading()
				resolve(err)
			},
		});
	})
	return promise
}

function postaction(url, data) {
	var promise = new Promise((resolve, reject) => {
		let token = uni.getStorageSync("token")
		let userinfo = uni.getStorageSync("userinfo")
		if (!token) {
			uni.navigateTo({
				url: './pages/login/login',
			});
		}

		uni.request({
			url: api + url,
			method: 'POST',
			data: data,
			header: {
				'Content-Type': 'application/json;charset=UTF-8',
				'X-Access-Token': token,
			},
			success: res => {

				// uni.hideLoading()
				// console.log(res)
				resolve(res)
			},
			fail: (err) => {
				// uni.hideLoading()
				resolve(err)
			},
		})
	})
	return promise
}

function putaction(url, data) {
	var promise = new Promise((resolve, reject) => {
		let token = uni.getStorageSync("token")
		let userinfo = uni.getStorageSync("userinfo")
		if (!token) {
			uni.navigateTo({
				url: './pages/login/login',
			});
		}

		uni.request({
			url: api + url,
			method: 'PUT',
			data: data,
			header: {
				'Content-Type': 'application/json;charset=UTF-8',
				'X-Access-Token': token,
			},
			success: res => {
				// uni.hideLoading()
				resolve(res)
			},
			fail: (err) => {
				// uni.hideLoading()
				resolve(err)
			},
		})
	})
	return promise
}

function deleteaction(url, data) {
	var promise = new Promise((resolve, reject) => {
		let token = uni.getStorageSync("token")
		let userinfo = uni.getStorageSync("userinfo")
		if (!token) {
			uni.navigateTo({
				url: './pages/login/login',
			});
		}
		// uni.showLoading({
		// 	title: '加载中'
		// })
		uni.request({
			url: api + url,
			method: 'DELETE',
			data: data,
			header: {
				'Content-Type': 'application/json;charset=UTF-8',
				'X-Access-Token': token,
			},
			success: res => {
				// uni.hideLoading()
				resolve(res)
			},
			fail: (err) => {
				// uni.hideLoading()
				resolve(err)
			},
		})
	})
	return promise
}


module.exports = {
	getaction,
	postaction,
	putaction,
	deleteaction,
	Loginpost
}
